package 笔试.A2022拼多多;

import java.util.*;

public class second {
    public static void main(String arg[]){//main--来自高金磊
         Scanner scanner=new Scanner(System.in);
         int T=scanner.nextInt();
        for (int i = 0; i < T; i++) {
            int n=scanner.nextInt();
            int v=scanner.nextInt();
//            int[][] data=new int[n][4];//num, sta,end
            LinkedList<int[]> list=new LinkedList<int[]>();
            for (int j = 0; j < n; j++) {
                int[] data=new int[3];
                data[0]=scanner.nextInt();
                data[1]=scanner.nextInt();
                data[2]=scanner.nextInt();
                list.add(data);
            }

            //贪心
            int res=0;
            int day=1;
            while (!list.isEmpty())
            {
               LinkedList<int[]> middle=new LinkedList<>();
                int[] ints=new int[3];
               //收集当前已经成熟的
                for (int j = 0; j < list.size(); j++) {

                    ints = list.pollFirst();
                    if (ints[1]<=day){
                        middle.add(ints);
                    }
                    else {
                        list.addLast(ints);
                    }
                   
                }
                //评估紧迫性
                Collections.sort(middle, new Comparator<int[]>() {
                    @Override
                    public int compare(int[] o1, int[] o2) {
                        return o1[1]-o2[1];
                    }
                });

                //摘西瓜
                int max=v;
                for (int k = 0; k < middle.size(); k++) {
                    ints=middle.pollFirst();
                    if (ints[0]<max){
                        max-=ints[0];
                    }
                    else {
                        ints[0]-=max;
                        max=0;
                        middle.addLast(ints);
                        break;
                    }
                }
                res+=v-max;
                //将没有采摘的放回
                for (int[] ints1 : middle) {
                    if (ints[2]>day){
                        list.addLast(ints);
                    }
                }
                day++;
            }
            System.out.println(res);


        }
    }
}
